package com.ruoyi.play.service.impl;

import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.play.mapper.WhereToGoUserChoiceMapper;
import com.ruoyi.play.domain.WhereToGoUserChoice;
import com.ruoyi.play.service.IWhereToGoUserChoiceService;
import com.ruoyi.play.service.IWhereToGoOptionService;

/**
 * 用户选择记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-12-19
 */
@Service
public class WhereToGoUserChoiceServiceImpl implements IWhereToGoUserChoiceService 
{
    @Autowired
    private WhereToGoUserChoiceMapper whereToGoUserChoiceMapper;

    @Autowired
    private IWhereToGoOptionService whereToGoOptionService;

    /**
     * 查询用户选择记录
     * 
     * @param id 用户选择记录主键
     * @return 用户选择记录
     */
    @Override
    public WhereToGoUserChoice selectWhereToGoUserChoiceById(Long id)
    {
        return whereToGoUserChoiceMapper.selectWhereToGoUserChoiceById(id);
    }

    /**
     * 查询用户选择记录列表
     * 
     * @param whereToGoUserChoice 用户选择记录
     * @return 用户选择记录
     */
    @Override
    public List<WhereToGoUserChoice> selectWhereToGoUserChoiceList(WhereToGoUserChoice whereToGoUserChoice)
    {
        return whereToGoUserChoiceMapper.selectWhereToGoUserChoiceList(whereToGoUserChoice);
    }

    /**
     * 根据用户ID查询选择记录
     * 
     * @param userId 用户ID
     * @return 用户选择记录
     */
    @Override
    public List<WhereToGoUserChoice> selectWhereToGoUserChoiceByUserId(Long userId)
    {
        return whereToGoUserChoiceMapper.selectWhereToGoUserChoiceByUserId(userId);
    }

    /**
     * 根据模块ID查询选择记录
     * 
     * @param moduleId 模块ID
     * @return 用户选择记录
     */
    @Override
    public List<WhereToGoUserChoice> selectWhereToGoUserChoiceByModuleId(Long moduleId)
    {
        return whereToGoUserChoiceMapper.selectWhereToGoUserChoiceByModuleId(moduleId);
    }

    /**
     * 新增用户选择记录
     * 
     * @param whereToGoUserChoice 用户选择记录
     * @return 结果
     */
    @Override
    public int insertWhereToGoUserChoice(WhereToGoUserChoice whereToGoUserChoice)
    {
        return whereToGoUserChoiceMapper.insertWhereToGoUserChoice(whereToGoUserChoice);
    }

    /**
     * 修改用户选择记录
     * 
     * @param whereToGoUserChoice 用户选择记录
     * @return 结果
     */
    @Override
    public int updateWhereToGoUserChoice(WhereToGoUserChoice whereToGoUserChoice)
    {
        return whereToGoUserChoiceMapper.updateWhereToGoUserChoice(whereToGoUserChoice);
    }

    /**
     * 删除用户选择记录信息
     * 
     * @param id 用户选择记录主键
     * @return 结果
     */
    @Override
    public int deleteWhereToGoUserChoiceById(Long id)
    {
        return whereToGoUserChoiceMapper.deleteWhereToGoUserChoiceById(id);
    }

    /**
     * 批量删除用户选择记录
     * 
     * @param ids 需要删除的用户选择记录主键
     * @return 结果
     */
    @Override
    public int deleteWhereToGoUserChoiceByIds(Long[] ids)
    {
        return whereToGoUserChoiceMapper.deleteWhereToGoUserChoiceByIds(ids);
    }

    /**
     * 记录用户选择（包含更新选项热度值）
     * 
     * @param userId 用户ID
     * @param moduleId 模块ID
     * @param optionId 选项ID
     * @param ipAddress IP地址
     * @param deviceInfo 设备信息
     * @return 结果
     */
    @Override
    @Transactional
    public int recordUserChoice(Long userId, Long moduleId, Long optionId, String ipAddress, String deviceInfo)
    {
        // 创建用户选择记录
        WhereToGoUserChoice userChoice = new WhereToGoUserChoice();
        userChoice.setUserId(userId);
        userChoice.setModuleId(moduleId);
        userChoice.setOptionId(optionId);
        userChoice.setChoiceTime(new Date());
        userChoice.setIpAddress(ipAddress);
        userChoice.setDeviceInfo(deviceInfo);
        
        // 插入用户选择记录
        int result = insertWhereToGoUserChoice(userChoice);
        
        if (result > 0) {
            // 更新选项热度值
            whereToGoOptionService.updateOptionPopularity(optionId);
        }
        
        return result;
    }
} 